/**
 * 服务区域 - 编辑页面
 * @author liuw
 * @Date 2019/09/05
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            isDetail: false,
            form: '',
            rules: {
                MMRE001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRE003: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRE004: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ]
            },
            selectOptions:{
                base_gwai640_mmre003 : app.datas_('base_gwai640_mmre003'),
                sys_y_n : app.datas_('sys_y_n')
            }
        },
        callObj,
        moduleName,
        layOpenIndex,pageVue
    ;

    var gwai640Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai640_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },
        editData: function () {
            app.ajax({
                xurl: 'base/gwai640/editData',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        // callObj.update(result.xdata);
                        app.pageModule('gwai640').reloadTableCurr();
                        self.closePage();
                    }
                }
            });
        },
        addData: function () {
            app.ajax({
                xurl: 'base/gwai640/addData',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwai640').reloadTableCurr();
                        self.closePage();
                    }
                }
            });
        },
        closePage: function(){
            layer.close(layOpenIndex);
        },
        _call: function (tagData) {
            pageVue.$refs.form.resetFields();
            moduleName = tagData.moduleName;
            layOpenIndex = tagData.layOpenIndex;

            if(tagData.is_detail){
                vueData.isDetail = true;
            }else {
                vueData.isDetail = false;
            }

            if (tagData.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        MMRE001: '',
                        MMRE002: '',
                        MMRE003: '1',
                        MMRE004: '',
                        MMREACTI: 'Y'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['base_gwai640']);
            pageVue = new Vue({
                el: '#gwai640_edit',
                data: vueData,
                methods: {
                    selectOrg: function(){
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_sysorg_id',
                                    columnList: [
                                        {label: '组织编号', prop: 'ID', width: '150'},
                                        {label: '组织名称', prop: 'NAME', width: '150'}
                                    ],
                                },
                                cb:function (data) {
                                    vueData.form.MMRE001 = data.ID;
                                    vueData.form.MMRE001_NAME = data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择归属组织信息',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    selectArea: function(){
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwad002',
                                    searchParam: {
                                        GWAD001: Number(vueData.form.MMRE003)
                                    },
                                    columnList: [
                                        {label: '区域编号', prop: 'GWAD002', width: '150'},
                                        {label: '区域名称', prop: 'GWADL004', width: '150'},
                                        {label: '助记码', prop: 'GWADL005', width: '150'}
                                    ],
                                },
                                cb:function (data) {
                                    vueData.form.MMRE004 = data.GWAD002;
                                    vueData.form.MMRE004_NAME = data.GWADL004;
                                },
                            },
                            index: 0,
                            openOpts: {
                                title: '选择服务区域',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    cancel: function () {
                        self.closePage();
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                            }
                        });
                    },
                    changeMMRE003:function () {
                        vueData.form.MMRE004 = "";
                        vueData.form.MMRE004_NAME = "";
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('gwai640_edit');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new gwai640Edit();

});